﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit.MEP.Opening.Common;
using HYRMEOpening;

namespace YArchitech.Revit.MEP.Opening
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdCreateOpening : IExternalCommand
	{
		public void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
			string transactionName = failuresAccessor.GetTransactionName();
			IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
			if (failureMessages.Count == 0)
			{
				e.SetProcessingResult((FailureProcessingResult)0);
				return;
			}
			if (transactionName.Equals(this.m_TransactionName))
			{
				foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
				{
					failuresAccessor.ResolveFailure(failureMessageAccessor);
				}
				e.SetProcessingResult((FailureProcessingResult)1);
				return;
			}
			e.SetProcessingResult((FailureProcessingResult)0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			string pathName = cmdData.Application.ActiveUIDocument.Document.PathName;
			if (pathName == "")
			{
				MessageBox.Show("请先保存项目。");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			cmdData.Application.Application.FailuresProcessing += this.OnFailuresProcessing;
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Result result;
			try
			{
				if (CmdCreateOpening._hWndRevit == null)
				{
					CmdCreateOpening._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				ConfigDataXMLSelectForm configDataXMLSelectForm = new ConfigDataXMLSelectForm(false);
				string str = XmlUtils.SplitPathName(pathName) + ".xml";
				configDataXMLSelectForm.FileName = Product.UserDataLocation + "\\" + str;
				if (configDataXMLSelectForm.ShowDialog() == DialogResult.OK)
				{
					CmdCreateOpening.m_lineStyleOpening2D = CmdTools.GetOpening2DLineStyle(document);
					CmdCreateOpening.m_linePattern = CmdTools.GetOpening2DLineStylePattern(document);
					CmdCreateOpening.m_filledRegionType = CmdTools.GetOpening2DFilledRegionType(document);
					bool server = false;
					CmdCreateOpening.FileName = configDataXMLSelectForm.FileName;
					ShowDlgApp.ThisApp.ShowDlgCreateOpening(cmdData.Application, server, CmdCreateOpening.FileName);
					ShowDlgApp.ThisApp.mDlgCreate.cmdData = cmdData;
					for (;;)
					{
						try
						{
							activeUIDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new OpeningTagFilter(), "");
							continue;
						}
						catch (Exception)
						{
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						break;
					}
				}
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		private List<DbOpeningBase> GetInvalidOpenings(Autodesk.Revit.DB.Document doc, string strXMLPath)
		{
			FileSystemInfo fileSystemInfo = new FileInfo(strXMLPath);
			List<DbOpeningBase> list = new List<DbOpeningBase>();
			if (fileSystemInfo.Exists)
			{
				try
				{
					string strDocFileName = XmlUtils.SplitPathName(doc.PathName);
					XmlDocument xmlDocument = new XmlDocument();
					xmlDocument.Load(strXMLPath);
					foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, strDocFileName, false))
					{
						XmlNode xmlNode = (XmlNode)obj;
						string name = xmlNode.Name;
						if (!(name == "梁洞"))
						{
							if (!(name == "板洞"))
							{
								if (!(name == "墙洞"))
								{
									continue;
								}
								goto IL_126;
							}
						}
						else
						{
							IEnumerator enumerator2 = xmlNode.GetEnumerator();
							{
								while (enumerator2.MoveNext())
								{
									object obj2 = enumerator2.Current;
									DbOpeningBase dbOpeningBase = XmlUtils.ReadBeamOpening((XmlNode)obj2);
									dbOpeningBase.Status = OpeningStatus.Invalid;
									list.Add(dbOpeningBase);
								}
								continue;
							}
						}
						IEnumerator enumerator3 = xmlNode.GetEnumerator();
						{
							while (enumerator3.MoveNext())
							{
								object obj3 = enumerator3.Current;
								DbOpeningBase dbOpeningBase = XmlUtils.ReadFloorOpening((XmlNode)obj3);
								dbOpeningBase.Status = OpeningStatus.Invalid;
								list.Add(dbOpeningBase);
							}
							continue;
						}
						IL_126:
						foreach (object obj4 in xmlNode)
						{
							DbOpeningBase dbOpeningBase = XmlUtils.ReadWallOpening((XmlNode)obj4);
							dbOpeningBase.Status = OpeningStatus.Invalid;
							list.Add(dbOpeningBase);
						}
					}
				}
				catch
				{
				}
			}
			return list;
		}

		public static GraphicsStyle m_lineStyleOpening2D = null;

		public static LinePatternElement m_linePattern = null;

		public static FilledRegionType m_filledRegionType = null;

		public static string FileName = "";

		private string m_TransactionName = "RotateElement";

		public static WindowHandle _hWndRevit = null;
	}
}
